package com.cmj.controller;

import com.cmj.domain.ResponseResult;
import com.cmj.domain.dto.CategoryDto;
import com.cmj.domain.entity.Category;
import com.cmj.domain.vo.CategoryVo;
import com.cmj.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /***
     * 获取分类表（名字）
     * @return 返回列表
     */
    @GetMapping("/list")
    public ResponseResult getCategoryList(Integer pageNum, Integer pageSize, CategoryDto categoryDto){
       return categoryService.getCategoryList(pageNum,pageSize,categoryDto);
    }

    @PostMapping
    public ResponseResult<CategoryVo> addCategory(@RequestBody Category Category){
        return categoryService.addCategory(Category);
    }

    //删除
    @DeleteMapping("{id}")
    public ResponseResult delCategory(@PathVariable("id") Long[] id){
        return categoryService.delCategory(id);
    }

    //获取更新项信息
    @GetMapping("{id}")
    public ResponseResult<CategoryVo> getCategory(@PathVariable("id") Long id){
        return categoryService.getCategory(id);
    }

    //更新信息
    @PutMapping
    public ResponseResult updateCategory(@RequestBody Category Category){
        return categoryService.updateCategory(Category);
    }

    //到处execl
    //限制用户权限
    @PreAuthorize("@ps.hasPermissions('content:category:export')")
    @GetMapping("/export")
    public void exportCategory(HttpServletResponse response) throws IOException {
        categoryService.exportCategory(response);
    }

    @GetMapping("/listAllCategory")
    public ResponseResult<CategoryVo> getListAllCategory(){
        return categoryService.getListAllCategory();
    }
}